import tiyData from "./detail_data";

/**
 * 发送
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
    tiyData.tData.emailDetail.title = "",
    tiyData.tData.emailDetail.content = ""

};



/**
 * 保存标签
 * @param self
 * @param tiyData
 * @param type 1 邮件标签 2通讯录标签
 */
export const saveTags = (self, tiyData,type,tags) => {
  let params = {
    id:self.id,
    type:type,//邮件标签
    tags: tags,
  };
  self.$Spin.show();
  self.$apiEmail.updateEmailTags(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("保存标签成功!");
      if(type===1){
        self.$parent.$parent.init();
        self.$parent.$parent.search();
      }
      if(type===2){
        self.search();
      }
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

/**
 * 关联
 * @param self
 * @param email
 * @param type 1 邮件详情关联用户  2通讯录关联用户
 */
export const manualRelate = (self, tiyData,emailAddressId,type) => {
  let params = {
    id:emailAddressId,
    type:1,
    userId: tiyData.tStatus.userNo.userId,
    encodeName: tiyData.tStatus.userNo.encodeName.trim(),
  };
  self.$Spin.show();
  self.$apiEmail.updateEmailAddress(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      if(tiyData.tStatus.userNo.userId==''){
        self.userNo=tiyData.tStatus.userNo.encodeName.trim();
      }
      tiyData.tStatus.userNo.encodeNameList.forEach(function(item){
        if(item.id==tiyData.tStatus.userNo.userId){
          self.userNo=item.encodeName;
        }
      });
      tiyData.tStatus.userNo.isShow=false;
      if(type==1){
        self.$parent.$parent.search();
      }
      if(type==2){
        self.search();
      }
      self.$Message.success("关联成功!");
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}


/**
 * 关联用户
 * @param self
 * @param email
 * @param type 1 邮件详情关联用户  2通讯录关联用户
 */
export const relatedUser = (self,tiyData, email,emailAddressId,type) => {
  let params = {
    id:emailAddressId,
    emailFrom:email,
  };
  self.$Spin.show();
  self.$apiEmail.relatedUser(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      let userList=response.result;
      if(userList.length==1){
        self.userNo=userList[0].encodeName;
        self.$Message.success(`自动关联成功,用户编号为：${userList[0].encodeName}`);
      }
      if(userList.length==0){
        tiyData.tStatus.userNo.userId='';
        tiyData.tStatus.userNo.encodeName='';
        tiyData.tStatus.userNo.isSelect=true;
        tiyData.tStatus.userNo.isShow=true;
        tiyData.tStatus.userNo.msg = '该邮箱在系统内不存在用户编号，你可以手动关联';
      }
      if(userList.length>1){
        tiyData.tStatus.userNo.encodeNameList=[...userList];
        tiyData.tStatus.userNo.userId='';
        tiyData.tStatus.userNo.encodeName='';
        tiyData.tStatus.userNo.isSelect=false;
        tiyData.tStatus.userNo.isShow=true;
        tiyData.tStatus.userNo.msg ='该邮箱匹配到多个用户编号，您可以选择一个';
      }
      if(type==1){
        self.$parent.$parent.search();
      }
      if(type==2){
        self.search();
      }
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}




